home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / util / time / backclock.lha / backclock / sources / style / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-16  |  10.2 KB  |  336 lines

  1. /*****************************************************************************
  2.  *
  3.  * Filename              : main.c
  4.  * Desc.                 : The main part of the style prog (init+save/load)
  5.  *
  6.  * version               : 1.1
  7.  * created               :    07-99
  8.  * last change           : 05-08-99
  9.  *
  10.  * history
  11.  * 1.0                   : initial version
  12.  * 1.1                   : some bugs removed (clear)
  13.  *
  14.  *****************************************************************************
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <intuition/intuition.h>
  20. #include <intuition/screens.h>
  21. #include <libraries/gadtools.h>
  22. #include <clib/intuition_protos.h>
  23. #include <clib/exec_protos.h>
  24. #include <clib/graphics_protos.h>
  25. #include <clib/rtracker_protos.h>
  26. #include <clib/exec_protos.h>
  27. #include <clib/dos_protos.h>
  28. #include <clib/gadtools_protos.h>
  29. #include <pragmas/rtracker_pragmas.h>
  30. #include <exec/libraries.h>
  31. #include <exec/memory.h>
  32. #include <dos/dos.h>
  33.  
  34. #include <libraries/pm.h>
  35. #include <proto/pm.h>
  36.  
  37. #include "main.h"
  38. #include "nodes.h"
  39. #include "hchoice.h"
  40. #include "graph.h"
  41. #include "menu.h"
  42.  
  43.  
  44. /* the base libraries
  45.  */
  46. struct Library        * RTrackerBase ;
  47. struct Library        * GadToolsBase ;
  48. struct PopupMenuBase  * PopupMenuBase;
  49.  
  50. void ProcessWin(struct Window *) ;                   /* handle window events */
  51. void SaveFile(char * filename) ;            /* save a file to env: & envarc: */
  52. void SavePoints(void) ;
  53.  
  54. SPoint * ipoint,                                        /* the initial point */
  55.        * current ;                      /* the current point (for inserting) */
  56.  
  57. char * version = VER ;                                    /* the $VER string */
  58.  
  59. /*
  60.  * defintion of the gadget
  61.  * should be cleaned for future version
  62.  *
  63.  */
  64. struct NewGadget savgad = {
  65.   0,0,WIDTH, 20, "Save", NULL, ID_SAVE, PLACETEXT_IN, NULL, NULL
  66. } ;
  67. struct NewGadget clrgad = {
  68.   0,0,WIDTH, 20, "Clear", NULL, ID_CLEAR, PLACETEXT_IN, NULL, NULL
  69. } ;
  70.  
  71. /* The MAIN part :)
  72.  */
  73. void main() {
  74.   APTR VI ;                                               /* the visual info */
  75.   struct Screen * scr ;                                     /* the wb screen */
  76.   struct Window * wing ;                                  /* the main window */
  77.   struct Gadget * gad,                                        /* the gadgets */
  78.                 * glist ;
  79.  
  80.   /* initialisation
  81.    */
  82.   if ((RTrackerBase = OpenLibrary("rtracker.library",10)) == NULL) {
  83.     /* what no rtracker:
  84.      * should quit safely
  85.      */
  86.     printf("Couldn't open rtracker.library version 1\n") ;
  87.     exit(20) ;                                                      /* fatal */
  88.   }
  89.   /* initialize the library
  90.    */
  91.   InitResource() ;
  92.   if ((GadToolsBase = NewOpenLibrary("gadtools.library",37)) == NULL) {
  93.     /* what no gadtools
  94.      * how do i display my gadgets then ?
  95.      */
  96.     printf("Couldn't open gadtools.library version 37\n") ;
  97.     CloseResource() ;
  98.     CloseLibrary(RTrackerBase) ;
  99.     exit(20) ;
  100.   }
  101.   if ((PopupMenuBase=(struct PopupMenuBase *)NewOpenLibrary(POPUPMENU_NAME,
  102.                                                             POPUPMENU_VERSION)) == NULL) {
  103.     /* the popup library is inexistant
  104.      * so -> quit
  105.      */
  106.     printf("Couldn't open %s version %ld\n", POPUPMENU_NAME,
  107.                                              POPUPMENU_VERSION) ;
  108.     CloseResource() ;
  109.     /* i know i did not close GadTools
  110.      * but rtracker did it for me (just before)
  111.      */
  112.     CloseLibrary(RTrackerBase) ;
  113.     exit(20) ;
  114.   }
  115.   if (scr = LockPubScreen("Workbench")) {
  116.     /* pub screen found (normaly the wb)
  117.      */
  118.     if (VI = NewGetVisualInfo(scr, TAG_DONE)) {
  119.       /* got the vi for the gadgets
  120.        */
  121.       if (gad = NewCreateContext(&glist)) {
  122.         /* creating gadgets
  123.          * this will cleaned in the future
  124.          */
  125.         savgad.ng_LeftEdge = scr->WBorLeft ;
  126.         savgad.ng_TopEdge  = scr->RastPort.TxHeight + scr->WBorTop + 4 + HEIGHT ;
  127.         savgad.ng_VisualInfo = VI ;
  128.         gad = CreateGadgetA(BUTTON_KIND, gad, &savgad, TAG_DONE) ;
  129.         clrgad.ng_LeftEdge = scr->WBorLeft ;
  130.         clrgad.ng_TopEdge  = scr->RastPort.TxHeight + scr->WBorTop + 8 + HEIGHT + GADHEIGHT ;
  131.         clrgad.ng_VisualInfo = VI ;
  132.         gad = CreateGadgetA(BUTTON_KIND, gad, &clrgad, TAG_DONE) ;
  133.         /* open the window
  134.          * note that default values will be #define'd in the future
  135.          */
  136.         wing = OpenWindowTags(NULL,WA_Left, 50,
  137.                                    WA_Top,  50,
  138.                                    WA_InnerWidth, WIDTH,
  139.                                    WA_InnerHeight, HEIGHT+50,
  140.                                    WA_IDCMP, IDCMP,
  141.                                    WA_Flags, FLAGS,
  142.                                    WA_Title, TITLE,
  143.                                    WA_Gadgets, glist,
  144.                                    WA_RMBTrap, TRUE,
  145.                                    WA_ScreenTitle, STITLE, TAG_DONE) ;
  146.  
  147.         if (wing) {
  148.           /* window opened
  149.            * initialize the first point (which is not a point)
  150.            */
  151.           ipoint = NewAllocVec(sizeof(SPoint), MEMF_PUBLIC|MEMF_CLEAR) ;
  152.           ipoint->next = NULL ;                       /* the fist & the last */
  153.           current = ipoint ;                        /* set the current point */
  154.           DrawGrid(wing) ;                                  /* draw the grid */
  155.           ProcessWin(wing) ;                      /* & handles window events */
  156.           CloseWindow(wing) ;
  157.         }else
  158.           /* something goes wrong ?
  159.            */
  160.           printf("Can't open window\n") ;
  161.         NewFreeGadgets(glist) ;                     /* this free all gadgets */
  162.       }else
  163.         printf("Can't create context\n") ;
  164.       NewFreeVisualInfo(VI) ;
  165.     }else
  166.       printf("Can't get visual info\n") ;
  167.     UnlockPubScreen(NULL, scr) ;
  168.   }else
  169.     printf("Can't lock public screen Workbench\n") ;
  170.   CloseResource() ;                          /* clear all forgotten resource */
  171.   CloseLibrary(RTrackerBase) ;
  172. }
  173.  
  174. /*
  175.  * this opens the choice window &
  176.  * ask the type of arrow
  177.  * then save it to disk
  178.  */
  179. void SavePoints() {
  180.   LONG type ;                                                   /* h | m | s */
  181.  
  182.   type = choice() ;                             /* ask for the type of arrow */
  183.  
  184.   switch (type) {
  185.     case ASECONDS :
  186.       SaveFile("env:backclock.s") ;
  187.       SaveFile("envarc:backclock.s") ;
  188.       break ;
  189.  
  190.     case AMINUTES :
  191.       SaveFile("env:backclock.m") ;
  192.       SaveFile("envarc:backclock.m") ;
  193.       break ;
  194.  
  195.     case AHOURS :
  196.       SaveFile("env:backclock.h") ;
  197.       SaveFile("envarc:backclock.h") ;
  198.       break ;
  199.  
  200.     default :
  201.       break ;
  202.   }
  203.  
  204.    /*
  205.   }   */
  206.  
  207. }
  208.  
  209. /*
  210.  * this save a single file to disk
  211.  */
  212. void SaveFile(char * filename) {
  213.   BPTR file ;                                    /* the file, null if failed */
  214.   SPoint * cp ;                                     /* the point to be saved */
  215.   file = NewOpen(filename, MODE_NEWFILE) ;                  /* open the file */
  216.   if (file) {
  217.     /* the file is opened
  218.      */
  219.     cp = ipoint->next ;                              /* skip the first point */
  220.     while(cp) {
  221.       /* write datas to disk
  222.        */
  223.       Write(file, &(cp->x), 4) ;
  224.       Write(file, &(cp->y), 4) ;
  225.       cp = cp->next ;
  226.     }
  227.     NewClose(file) ;
  228.   }
  229. }
  230.  
  231.  
  232. void ProcessWin(struct Window * win) {
  233.   /* handle events
  234.    */
  235.   BOOL stop = FALSE ;                                         /* TRUE = QUIT */
  236.   struct IntuiMessage * imsg ;                          /* the intui message */
  237.   ULONG class ;                                                  /* for temp */
  238.   UWORD code ;
  239.   WORD mx, my ;                         /* coords of the mouse in the window */
  240.   struct Gadget * gad ;                          /* gadget currently pressed */
  241.   ULONG r ;                          /* choice of the user for the popupmenu */
  242.   BOOL draw = NULL ;                      /* if !NULL then redraw the window */
  243.  
  244.   while(!stop) {
  245.     /* the user didn't choose the close button
  246.      */
  247.     WaitPort(win->UserPort) ;                          /* wait for a message */
  248.     while (imsg = (struct IntuiMessage*)GetMsg(win->UserPort)) {
  249.  
  250.       /* extract datas from the message
  251.        */
  252.       class = imsg->Class ;
  253.       code  = imsg->Code ;
  254.       mx = imsg->MouseX ;
  255.       my = imsg->MouseY ;
  256.       gad = (struct Gadget*)imsg->IAddress ;
  257.       if (imsg) {
  258.         ReplyMsg((struct Message*)imsg) ;
  259.         switch (class) {
  260.           case IDCMP_CLOSEWINDOW :
  261.             /* close gadget selected
  262.              */
  263.             stop=TRUE ;
  264.             break ;
  265.           case IDCMP_MOUSEBUTTONS :
  266.             /* boutons actived
  267.              */
  268.             switch (code) {
  269.               case 104 : /* 232 = released */
  270.                 /* lbutton pressed
  271.                  */
  272.                 AddPoint(win, mx, my) ;
  273.                 draw = TRUE ;
  274.                 break ;
  275.  
  276.               case 105 :
  277.                 /* rbutton pressed
  278.                  */
  279.                 r = OpenMenu(win, code) ;
  280.                 switch (r) {
  281.                   case ID_MDELETE:
  282.                   /* delete this point
  283.                    */
  284.                     RemPoint(win, mx, my) ;
  285.                     draw = TRUE ;
  286.                     break;
  287.                   case ID_MBECURRENT:
  288.                   /* be the current point
  289.                    */
  290.                     SetCurPoint(win, mx, my) ;
  291.                     draw = TRUE ;
  292.                     break;
  293.                   case ID_MQUIT:
  294.                   /* quit
  295.                    */
  296.                     stop = TRUE ;
  297.                     break ;
  298.  
  299.                   default:
  300.                     break ;
  301.                 }
  302.                 break ;
  303.  
  304.               default :
  305.                 break ;
  306.             }
  307.             if (draw) {
  308.               DrawArrow(win) ;
  309.               draw = NULL ;
  310.             }
  311.             break ;
  312.           case IDCMP_GADGETUP :
  313.             /* gadget pressed
  314.              */
  315.             switch (gad->GadgetID) {
  316.               case ID_SAVE:
  317.                 /* Save the file
  318.                  */
  319.                 SavePoints() ;
  320.                 break;
  321.               case ID_CLEAR:
  322.                 /* clear all the points
  323.                  */
  324.                 ClearPoints() ;
  325.                 DrawGrid(win) ;
  326.                 break;
  327.             }
  328.             break ;
  329.           default:
  330.             break ;
  331.         }
  332.       }
  333.     }
  334.   }
  335. }
  336.